package com.yidu.ladder.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yidu.ladder.system.dao.CustomerDao;
import com.yidu.ladder.system.domain.Customer;
import com.yidu.ladder.system.domain.CustomerVo;
import com.yidu.ladder.system.service.CustomerService;
import com.yidu.ladder.util.IDGenerator;

/**
 * 客户管理业务层实现类
 * @author Xiaofei
 *
 */
@Service
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private CustomerDao customerDao;

	public CustomerDao getCustomerDao() {
		return customerDao;
	}

	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}

	@Override
	public Map<String, Object> queryCustomerByCriteriaWithPage(CustomerVo paramCustomerVo) {
		//设置页面
		paramCustomerVo.setPage((paramCustomerVo.getPage() -1) * paramCustomerVo.getLimit());
		//调用Dao层方法分页查询
		List<Customer> customerList = customerDao.queryCustomerByCriteriaWithPage(paramCustomerVo);
		//每次查询得到相应的记录数
		int customerCount = customerDao.countCustomerByCriteria(paramCustomerVo);
		//创建一个Map接收数据返回给前端
		Map<String, Object> returnMap = new HashMap<String, Object>();
		// returnMap存放code
		returnMap.put("code", 0);
		// returnMap存放count
		returnMap.put("count", customerCount);
		// returnMap存放数据
		returnMap.put("data", customerList);
		// 返回一个returnMap
		return returnMap;
	}

	@Override
	public Map<String, Object> selectAll() {
		//调用Dao层方法查询所有
		List<Customer> list = customerDao.selectAll();
		//创建一个Map接收数据返回给前端
		Map<String, Object> map = new HashMap<String, Object>();
		//map存放code
		map.put("code", 0);
		//map存放count
		map.put("data", list);
		//map存放数据
		map.put("count", 10);
		//返回一个map
		return map;
	}
	
	@Override
	public boolean addCustomer(Customer customer) {
		// 为新增的客户设置Id
		customer.setCustomerId(IDGenerator.getId());
		//添加时是否可用字段都为是
		customer.setUseable(1);
		//调用dao层添加的方法
		return customerDao.addCustomer(customer)== 1?true : false;
	}

	@Override
	public boolean updateCustomer(Customer customer) {
		System.out.println(customer);
		//修改基础档案，如果等于1就返回true，否则返回false
		return customerDao.updateCustomer(customer) == 1?true : false;
	}

	@Override
	public boolean freezeOrRecoverCustomer(String customerId, int useable) {
		//创建传参的Map集合
		Map<String, Object> paramMap = new HashMap<String, Object>();
		//判断进行冻结还是还原
		int customerStatus = useable;
		//根据逗号分割ID
		String[] customerIds = customerId.split(",");
		//定义执行结果返回的总数 因为成功返回1，所以把所有执行结果加起来正好就等于ID的总数
		int total = 0;
		//遍历customerIds数组
		for (int i = 0; i < customerIds.length; i++) {
			// 添加Id
			paramMap.put("customerId", customerIds[i]);
			//添加要修改的状态
			paramMap.put("useable", customerStatus);
			//执行
			int result = customerDao.freezeOrRecoverCustomer(paramMap);
			//计数
			total += result;
		}
		//返回计数
		return total == customerIds.length ? true : false;
	}

}
